package com.hitao.pd.controller;

import com.hitao.Model.packageModel;
import com.hitao.util.FileScan;
import com.hitao.util.Ipa;
import com.hitao.util.IpaParser;
import com.hitao.util.PlistGenerate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by justin on 16/4/1.
 */
@Controller
public class IndexController {
    @Autowired
    private HttpServletRequest request;

    @RequestMapping("")
    public String indexView(Model model) {
        File file = new File("");
        model.addAttribute("strKey" , file.getAbsolutePath());
        return "index";
    }
    @RequestMapping("newPackage/{packageName}")
    public String newpackageView(@PathVariable String packageName) throws IOException {
        File f = new File("");
        String curpath = f.getAbsolutePath();
        String ipaPath = "D:/appweb/ios/"+packageName+".ipa";
        IpaParser parser = new IpaParser();
        File file = new File(ipaPath);
        System.out.println(file.getTotalSpace());
        Ipa ipa = parser.parse(file);
        System.out.println(ipa);
        PlistGenerate gplist = new PlistGenerate();
        String domain = "http://"+request.getServerName() + ":" +request.getServerPort() + "/" + request.getContextPath() + "/ios/";
        gplist.createPlist(packageName, domain+packageName+".ipa", domain+"icon.png", ipa.getBundleIdentifier(), ipa.getBundleShortVersionString(), ipa.getBundleDisplayName(), "D:/appweb/plist/");
        return "index";
    }

    @RequestMapping("iosinstall/{plistfilename}")
    public String iosInstall(@PathVariable String plistfilename, Model model) {
        String url = "itms-services://?action=download-manifest&url=https://" + request.getServerName() + ":" + "8443" + request.getContextPath() + "/plist/" + plistfilename + ".plist";
        //String url = "http://www.baidu.com";
        model.addAttribute("url",url);
        return "iosinstall";
    }

    @RequestMapping("package/{platform}")
    public String packageView(@PathVariable String platform, Model model) {
        FileScan myScan = new FileScan();
        List<packageModel> res = myScan.parseFile("D:/appweb/"+platform);
        SimpleDateFormat ft = new SimpleDateFormat ("yyyy.MM.dd-HH:mm:ss");
        //List<packageModel> res = myScan.parseFile("src/main/webapp/"+platform);
        for (int i=0;i<res.size();i++) {
            String filename = res.get(i).filename;
            res.get(i).downloadUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/"+ platform +"/" + filename;
            if (filename.indexOf('.') > 0) {
                //res.get(i).plistFilepath = "itms-services://?action=download-manifest&url=https://" + request.getServerName() + ":" + "8443" + request.getContextPath() + "/plist/" + filename.substring(0, filename.lastIndexOf('.')) + ".plist";
                if (platform.equals("ios")) {
                    //res.get(i).plistFilepath = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/iosinstall/" + filename.substring(0, filename.lastIndexOf('.')) + ".plist";
                    res.get(i).plistFilepath = "itms-services://?action=download-manifest&url=https://" + request.getServerName() + ":" + "8443" + request.getContextPath() + "/plist/" + filename.substring(0, filename.lastIndexOf('.')) + ".plist";
                } else {
                    res.get(i).plistFilepath = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/"+ platform +"/" + filename;
                }
            }
            res.get(i).timeDisplay = ft.format(res.get(i).updateTime);
        }
        /*List<packageModel> list = new ArrayList<packageModel>();
        for (int i=0;i<10;i++) {
            packageModel pk = new packageModel();
            pk.filename = platform+i;
            pk.updateTime = new Date(2010,3,12);
            list.add(pk);
        }*/
        String des = String.format("适用于%s设备", platform);
        model.addAttribute("des", des);
        model.addAttribute("newpkg", res.get(0));
        model.addAttribute("package" , res);
        return "package";
    }
}
